import 'dart:async';

import 'package:flutter/material.dart';

/// Auto self-rotating widget.
class SelfRotating extends StatefulWidget {
  const SelfRotating({super.key, required this.child});

  final Widget child;

  @override
  State<SelfRotating> createState() => _SelfRotatingState();
}

class _SelfRotatingState extends State<SelfRotating> {
  double _turns = 0.0;

  @override
  void initState() {
    super.initState();

    _rotate();
  }

  @override
  Widget build(BuildContext context) {
    return AnimatedRotation(
      onEnd: () {
        if (mounted) {
          setState(() => _turns++);
        }
      },
      turns: _turns,
      duration: const Duration(seconds: 1),
      child: widget.child,
    );
  }

  /// Triggers rotating animation.
  void _rotate() {
    Future.delayed(const Duration(milliseconds: 10), () {
      if (mounted) {
        setState(() => _turns = 1.0);
      }
    });
  }
}
